package com.example.demo.zaqizabatest;

import java.io.*;
import java.net.DatagramPacket;
import java.net.DatagramSocket;
import java.net.InetAddress;

/**
 * @ClassName SocketTest
 * @Author l42142
 * @Date 2021/3/5 10:45
 * @Description TODO
 * @Version 1.0
 **/
public class SocketTest {

    public static void main(String[] args) {
        // 一、TCP 协议

        // 测试 1

        /*Socket socket = null;
        OutputStream out = null;
        InputStream in = null;
        String serverIP = "127.0.0.1";    //服务器端 IP 地址
        int port = 5000;    //服务器端端口号
        try {
            socket = new Socket(serverIP, port);    //建立连接
            try {
                Thread.sleep(2000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            out = socket.getOutputStream();    //发送数据
            out.write("我是客户端数据 ".getBytes());
            byte[] b = new byte[1024];
            in = socket.getInputStream();
            int len = in.read(b);
            System.out.println(" 服务器端的反馈为：" + new String(b, 0, len));
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (in != null) {
                    in.close();
                }
                if (out != null) {
                    out.close();
                }
                if (socket != null) {
                    socket.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }*/


        // 测试2

        /*Socket socket = null;
        String serverIP = "127.0.0.1";    //服务器端 IP 地址
        int port = 5000;    //服务器端端口号
        PrintWriter out = null;
        BufferedReader in = null;
        try {
            socket = new Socket(serverIP, port);
            in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
            out = new PrintWriter(socket.getOutputStream(), true);
            while (true) {
                int number = (int) (Math.random() * 10) + 1;
                System.out.println("客户端正在发送的内容为：" + number);
                out.println(number);
                Thread.sleep(2000);
            }
        } catch (IOException | InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }*/


        // 测试3

        //循环100次
        /*for (int i = 0; i < 100; i++) {
            //创建客户端Socket
            Socket socket = null;
            //创建输入流
            ObjectOutputStream os = null;
            //创建输出流
            ObjectInputStream is = null;
            try {
                //连接服务器
                socket = new Socket("localhost", 10000);
                //接收输出流中的数据
                os = new ObjectOutputStream(socket.getOutputStream());
                //创建一个User对象
                User user = new User("user_" + i, "password_" + i);
                //将User对象写入输出流
                os.writeObject(user);
                os.flush();
                //接收输入流中的数据
                is = new ObjectInputStream(socket.getInputStream());
                //读取输入流中的数据
                Object obj = is.readObject();
                //如果数据不空则转换成User对象，然后输出成员信息
                if (obj != null) {
                    user = (User) obj;
                    System.out.println("user: " + user.getName() + "/" + user.getPassword());
                }
            } catch (IOException ex) {
                ex.printStackTrace();
            } catch (ClassNotFoundException e) {
                e.printStackTrace();
            } finally {
                try {
                    //关闭输入流
                    is.close();
                    //关闭输出流
                    os.close();
                    //关闭客户端
                    socket.close();
                } catch (Exception ex) {
                }
            }
        }*/

        // 二、UDP协议

        // 测试1

        DatagramSocket ds = null;
        DatagramPacket dpSend = null;
        InetAddress ia = null;
        int port = 3021;
        try {
            ia = InetAddress.getByName("127.0.0.1");
            ds = new DatagramSocket();
            for (int i = 0; i < 5; i++) {
                byte[] data = ("我是 UDP 客户端" + i).getBytes();
                dpSend = new DatagramPacket(data, data.length, ia, port);
                ds.send(dpSend);
                Thread.sleep(1000);
            }
            ds.close();
        } catch (IOException | InterruptedException e) {
            // TODO 自动生成的 catch 块
            e.printStackTrace();
        }
    }

}
